package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.entity.MyAcquisition;
import zhihuilan.entity.MyCircle;
import zhihuilan.entity.MyCircleDiscuss;
import zhihuilan.entity.MyStaff;
import zhihuilan.mapper.MyAcquisitionMapper;
import zhihuilan.mapper.MyCircleDiscussMapper;
import zhihuilan.params.query.MyCircleDiscussQueryParam;
import zhihuilan.service.MyAcquisitionService;
import zhihuilan.service.MyCircleDiscussService;
import zhihuilan.service.MyStaffService;

import java.util.Date;
import java.util.List;

@Service
public class MyCircleDiscussServiceImpl extends ServiceImpl<MyCircleDiscussMapper, MyCircleDiscuss> implements MyCircleDiscussService {

    @Autowired
    private MyStaffService myStaffService;

    @Override
    public Page<MyCircleDiscuss> selectPageList(MyCircleDiscussQueryParam queryParam) {
        QueryWrapper<MyCircleDiscuss> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
            }
            if (!ObjectUtils.isEmpty(queryParam.getPid())) {
                queryWrapper.eq("pid", queryParam.getPid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getPstaffid())) {
                queryWrapper.eq("pstaffid", queryParam.getPstaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getParentid())) {
                queryWrapper.eq("parentid", queryParam.getParentid());
            }
        }
        if (!ObjectUtils.isEmpty(queryParam.getOrderByFiled()) && !ObjectUtils.isEmpty(queryParam.getOrderByType())) {
            if ("asc".equals(queryParam.getOrderByType())) {
                queryWrapper.orderByAsc(queryParam.getOrderByFiled());
            }
            if ("desc".equals(queryParam.getOrderByType())) {
                queryWrapper.orderByDesc(queryParam.getOrderByFiled());
            }
        } else {
            queryWrapper.orderByDesc("create_time");
        }

        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyCircleDiscuss> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public List<MyCircleDiscuss> selectList(MyCircleDiscussQueryParam queryParam){
        QueryWrapper<MyCircleDiscuss> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
            }
            if (!ObjectUtils.isEmpty(queryParam.getPid())) {
                queryWrapper.eq("pid", queryParam.getPid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getPstaffid())) {
                queryWrapper.eq("pstaffid", queryParam.getPstaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getParentid())) {
                queryWrapper.eq("parentid", queryParam.getParentid());
            }
        }
        if (!ObjectUtils.isEmpty(queryParam.getOrderByFiled()) && !ObjectUtils.isEmpty(queryParam.getOrderByType())) {
            if ("asc".equals(queryParam.getOrderByType())) {
                queryWrapper.orderByAsc(queryParam.getOrderByFiled());
            }
            if ("desc".equals(queryParam.getOrderByType())) {
                queryWrapper.orderByDesc(queryParam.getOrderByFiled());
            }
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public MyCircleDiscuss selectById(Integer id) {
        MyCircleDiscuss myCircleDiscuss = this.baseMapper.selectById(id);
        return myCircleDiscuss;
    }

    @Override
    public Integer countRedDiscuss(MyCircleDiscuss myCircleDiscuss) {
        QueryWrapper<MyCircleDiscuss> wrapper = new QueryWrapper<>();
        wrapper.eq("pstaffid", myCircleDiscuss.getPstaffid());
        wrapper.eq("parentid", myCircleDiscuss.getParentid());
        wrapper.isNull("update_user");
        Long l = this.baseMapper.selectCount(wrapper);
        return l.intValue();
    }




    @Override
    public MyCircleDiscuss getMyCircleDiscussOne(MyCircleDiscuss myCircleDiscuss) {
        QueryWrapper<MyCircleDiscuss> wrapper = new QueryWrapper<>();
        wrapper.eq("pstaffid", myCircleDiscuss.getPstaffid());
        wrapper.eq("parentid", myCircleDiscuss.getParentid());
        wrapper.last("limit 1");
        wrapper.orderByDesc("id");
        MyCircleDiscuss m =  this.baseMapper.selectOne(wrapper);
        if(ObjectUtils.isEmpty(m)){
            return null;
        }
        MyStaff myStaff = myStaffService.selectMyStaffById(m.getStaffId());
        m.setStaffHeadimgur(myStaff.getLogo());
        m.setStaffTitle(myStaff.getTitle());
        return m;
    }

    @Override
    public Long selectCount(MyCircleDiscussQueryParam queryParam) {
        QueryWrapper<MyCircleDiscuss> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                wrapper.eq("staff_id", queryParam.getStaffId());
            }
            if (!ObjectUtils.isEmpty(queryParam.getParentid())) {
                wrapper.eq("parentid", queryParam.getParentid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getPid())) {
                wrapper.eq("pid", queryParam.getPid());
            }
        }
        Long count = this.baseMapper.selectCount(wrapper);
        return count;
    }


    @Override
    public MyCircleDiscuss addEntity(MyCircleDiscuss myCircleDiscuss) {

        this.baseMapper.insert(myCircleDiscuss);
        return myCircleDiscuss;
    }

    @Override
    public Boolean deletetById(Integer id) {
        int result = this.baseMapper.deleteById(id);
        return result > 0 ? true : false;
    }

    @Override
    public Boolean deletetByIdParam(Integer id, Integer staffId) {
        QueryWrapper<MyCircleDiscuss> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(staffId)) {
            queryWrapper.eq("staff_id", staffId);
        }
        if (!ObjectUtils.isEmpty(id)) {
            queryWrapper.eq("id", id).or().eq("parentid", id);
        }
        int result = this.baseMapper.delete(queryWrapper);
        return result > 0;
    }

    /**
     * 更新用户动态信息为已读；
     *
     * @param pid      该参数和pstaffid参数只能同时传入一个；根据推荐信息Id更新对应评论为已读；
     * @param staffId  当前操作用户；
     * @param pstaffid 传入该参数将不再考虑是某一条推荐信息，是将该用户所有未读评论标记为已读；
     */
    @Override
    public Boolean updateReaded(Integer pid, Integer staffId, Integer pstaffid) {
        QueryWrapper<MyCircleDiscuss> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(staffId)) {
            return false;
        }
        if (!ObjectUtils.isEmpty(pid)) {
            queryWrapper.eq("pid", pid);
        } else if (!ObjectUtils.isEmpty(staffId)) {
            queryWrapper.eq("pstaffid", staffId);
            queryWrapper.isNull("update_user");
        } else {
            return false;
        }
        MyCircleDiscuss updateEntity = new MyCircleDiscuss();
        updateEntity.setUpdateTime(new Date().getTime() / 1000);
        updateEntity.setUpdateUser(staffId + "");
        this.baseMapper.update(updateEntity, queryWrapper);
        return true;
    }
}
